﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;

namespace Mal.Balades.Admin.Converters
{
    public class TabItemConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<TabItem> tabItems = new List<TabItem>();
            if (value != null)
            {
                if (value is IEnumerable<ITabItemViewModel>)
                {
                    IEnumerable<ITabItemViewModel> viewModels = (IEnumerable<ITabItemViewModel>)value;
                    foreach (ITabItemViewModel viewModel in viewModels)
                    {
                        Control content = ViewModelViewConverter.GetControl(viewModel);

                        TabItem tabItem = new TabItem();
                        tabItem.Header = viewModel.TabHeader;

                        if (content.Parent == null)
                        {
                            tabItem.Content = content;

                            tabItems.Add(tabItem);
                        }

                    }

                }
                else
                {
                    throw new InvalidCastException("ViewModel must implements ITabItemViewModel");
                }
            }

            return tabItems;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
